package com.wcfsy.wechat.wx.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.wcfsy.wechat.wx.entity.AdConfigInfo;
import com.wcfsy.wechat.wx.pagination.IPage;
import com.wcfsy.wechat.wx.pagination.impl.Page;
import com.wcfsy.wechat.wx.sys.dao.AdConfigDao;
import com.wcfsy.wechat.wx.util.FileUploadUtil;
import com.wcfsy.wechat.wx.util.JsonMessage;

/**
 * 广告管理
 * 
 * @author yiqiang
 *
 */
@Controller
@RequestMapping("/cms/advertise")
public class AdvertiseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Resource
	private AdConfigDao adConfigDao;
	@Resource
	private FileUploadUtil fileUploadUtil;

	/**
	 * 广告 查询
	 */
	@RequestMapping("/view/page")
	public String page() {
		return "advertise/advertise_list";
	}

	@RequestMapping("/view")
	@ResponseBody
	public IPage<AdConfigInfo> view(int page, int rows) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("index", page);
		paramMap.put("size", rows);
		paramMap.put("limit", "limit");
		return new Page<AdConfigInfo>(adConfigDao.findByParam(paramMap),
				adConfigDao.count(paramMap), page, rows);
	}

	@RequestMapping("/add/page")
	public String preAdd() {
		return "advertise/advertise_add";
	}

	@RequestMapping({ "/modify/page/{id}" })
	public ModelAndView preModify(@PathVariable("id") long id) {
		ModelAndView mav = new ModelAndView("advertise/advertise_modify");
		mav.addObject("data", adConfigDao.getById(id));
		return mav;
	}

	@RequestMapping({ "/modify" })
	@ResponseBody
	public JsonMessage modify(long id, String adName, String status,
			String showIndex, String linkUrl, String text,
			HttpServletRequest request) {
		AdConfigInfo config = new AdConfigInfo();
		config.setAdName(adName);
		config.setLinkUrl(linkUrl);
		config.setShowIndex(showIndex);
		config.setStatus(status);
		config.setText(text);
		config.setId(id);
		JsonMessage jsonMessage = new JsonMessage();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("imgUrl");
		try {
			if (file != null && !file.isEmpty()) {
				String imgUrl = fileUploadUtil.upload(file);
				config.setImgUrl(imgUrl);
			}
			adConfigDao.update(config);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("修改成功");
		} catch (Exception e) {
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("修改失败");
		}
		return jsonMessage;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public JsonMessage delete(long id) {
		JsonMessage jsonMessage = new JsonMessage();
		try {
			adConfigDao.deleteById(id);
			jsonMessage.setSuccess("succeed");
			jsonMessage.setMessage("删除成功");
		} catch (Exception e) {
			jsonMessage.setSuccess("error");
			jsonMessage.setMessage("删除失败");
		}
		return jsonMessage;
	}

	@RequestMapping({ "/add" })
	@ResponseBody
	public JsonMessage add(String adName, String status, String showIndex,
			String linkUrl, String text, HttpServletRequest request)
			throws IOException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartRequest.getFile("imgUrl");
		try {
			String imgUrl = fileUploadUtil.upload(file);
			AdConfigInfo config = new AdConfigInfo();
			config.setAdName(adName);
			config.setLinkUrl(linkUrl);
			config.setShowIndex(showIndex);
			config.setStatus(status);
			config.setText(text);
			config.setImgUrl(imgUrl);
			adConfigDao.add(config);
			return JsonMessage.getSuccessJson("新增成功");
		} catch (Exception e) {
			logger.error("增加功能发生出错", e);
			return JsonMessage.getErrorJson("新增异常");
		}
	}

}
